﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.ShopServiceReference;
using Server;
using ServerApp;

namespace Client
{
    public class OrdersPresenter
    {
        #region Private members

        private IOrdersView m_ordersView;

        private IOrdersModel m_ordersModel;

        #endregion Private members


        #region Constructor

        /// <summary>
        /// The constructor of the class
        /// </summary>
        /// <param name="ordersView">IOrdersView object</param>
        public OrdersPresenter(IOrdersView ordersView)
        {
            //set view
            m_ordersView = ordersView;

            m_ordersView.OnCreate += new EventHandler(m_ordersView_OnCreate);
            m_ordersView.OnGetFullOrder += new EventHandler(m_ordersView_OnGetFullOrder);
            m_ordersView.OnReserveOrder += new ReserveOrder(m_ordersView_OnReserveOrder);
        }
       
        #endregion Constructor

        /// <summary>
        /// Reserves the order (status is changing to work)
        /// </summary>
        /// <param name="orderId">Order id</param>
        /// <param name="executorId">Id of the executor</param>
        private void m_ordersView_OnReserveOrder(long orderId, int executorId)
        {
            if(m_ordersModel == null)
                m_ordersModel = new OrdersMock();
            OrderFull order = m_ordersModel.ReserveOrder(orderId, executorId);
            m_ordersView.UpdateOrder(order);
        }


        /// <summary>
        /// Gets the list of the orders
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ordersView_OnCreate(object sender, EventArgs e)
        {
            if(m_ordersModel == null)
                m_ordersModel = new OrdersMock();
            List<OrderView> orderList = m_ordersModel.GetOrders(null, 1, 10);

            if(orderList != null)
            {
                m_ordersView.FillOrderTable(orderList);
            }            
        }


        /// <summary>
        /// Gets the order by its id
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void m_ordersView_OnGetFullOrder(object sender, EventArgs e)
        {
            OrdersEventArgs oea = e as OrdersEventArgs;   
        
            if(m_ordersModel == null)
                m_ordersModel = new OrdersMock();

            OrderFull order = m_ordersModel.GetOrder(oea.Id, oea.Language);
            m_ordersView.FillTheOrder(order);
        }
        
        
    }//class
}
